#include "main.h"
#include "qs_pwm.h"
#include "qs_config.h"
#include "qs_sched.h"
#include "qs_macb100_hal.h"

/**
  * @brief  Configure TIM1 PWM related GPIO
  * @param  None
  * @retval None
  */
static void APP_ConfigPWMChannel(void)
{
  LL_GPIO_InitTypeDef TIM1CH1MapInit= {0};
  LL_TIM_OC_InitTypeDef TIM_OC_Initstruct ={0};

  /* Configure PA11/PA10/PA9 as TIM1_CH4/TIM1_CH3/TIM1_CH2 */
  TIM1CH1MapInit.Pin        = QS_M1_SPPED_PIN|QS_M2_SPPED_PIN|QS_M3_SPPED_PIN;
  TIM1CH1MapInit.Mode       = LL_GPIO_MODE_ALTERNATE;
  TIM1CH1MapInit.Alternate  = LL_GPIO_AF_2; 
  TIM1CH1MapInit.Speed      = LL_GPIO_SPEED_FREQ_HIGH;
  TIM1CH1MapInit.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  TIM1CH1MapInit.Pull       = LL_GPIO_PULL_NO;
  LL_GPIO_Init(GPIOA,&TIM1CH1MapInit);
	  
  TIM_OC_Initstruct.OCMode        = LL_TIM_OCMODE_PWM2;     /* Mode: PWM2 */
  TIM_OC_Initstruct.OCState       = LL_TIM_OCSTATE_ENABLE;  /* Channel enable */
  TIM_OC_Initstruct.OCPolarity    = LL_TIM_OCPOLARITY_HIGH; /* Active polarity: high level */
  TIM_OC_Initstruct.OCIdleState   = LL_TIM_OCIDLESTATE_LOW; /* Complementary channel active polarity: high level */
  /* Channel 4 compare value: 100 */
  TIM_OC_Initstruct.CompareValue  = 600;
  /* Configure channel 1 */
  LL_TIM_OC_Init(TIM1,LL_TIM_CHANNEL_CH4,&TIM_OC_Initstruct);
  /* Channel 3 compare value: 100 */
  TIM_OC_Initstruct.CompareValue  = 600;
  /* Configure channel 2 */
  LL_TIM_OC_Init(TIM1,LL_TIM_CHANNEL_CH3,&TIM_OC_Initstruct);
  /* Channel 2 compare value: 100 */
  TIM_OC_Initstruct.CompareValue  = 600;
  /* Configure channel 3 */
  LL_TIM_OC_Init(TIM1,LL_TIM_CHANNEL_CH2,&TIM_OC_Initstruct);
	
}

/**
  * @brief  Configure TIM base
  * @param  None 
  * @retval None
  */
static void APP_ConfigTIM1Base(void)
{
  /* Configure TIM1 */
  LL_TIM_InitTypeDef TIM1CountInit = {0};
 
  TIM1CountInit.ClockDivision       = LL_TIM_CLOCKDIVISION_DIV1;   /* No clock division */
  TIM1CountInit.CounterMode         = LL_TIM_COUNTERMODE_UP;       /* Up counting mode */
  TIM1CountInit.Prescaler           = 1;                      /* Prescaler value: 12 */
  TIM1CountInit.Autoreload          = 600-1;                       /* Autoreload value: 1200 */
  TIM1CountInit.RepetitionCounter   = 0;                           /* Repetition counter value: 0 */
  
  /* Initialize TIM1 */
  LL_TIM_Init(TIM1,&TIM1CountInit);
  
  /* Enable main output */
  LL_TIM_EnableAllOutputs(TIM1);

  /* Enable TIM1 counter */
  LL_TIM_EnableCounter(TIM1);
	

}

void qs_motor_pwm_duty_set(uint8_t idx,float duty)
{
  duty = (duty > 100) ? 0:600-(uint16_t)(600*duty/100);
	switch(idx)
	{
	  case 0:LL_TIM_OC_SetCompareCH2(TIM1,duty);
			break;
		case 1:LL_TIM_OC_SetCompareCH3(TIM1,duty);
			break;
		case 2:LL_TIM_OC_SetCompareCH4(TIM1,duty);
		
			break;
		default:
			break;
	}
}

void qs_pwm_init(void)
{
	LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_TIM1);
  APP_ConfigPWMChannel();
  APP_ConfigTIM1Base();
}





